﻿#region "Copyright (C) Lenny Granovsky. 2005-2013"
//This program is free software distribute under the terms of the GNU General Public License as published by
//the Free Software Foundation. Please read AssemblyInfo.cs file for more information.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Web.Enhancements.Rest
{
    /// <summary>
    /// <para>Interface that indicates class as being able to be rendered to JS Client and callable from Client as REST service. </para>
    /// <para>It also provides methods to build JS Client and related components. </para>
    /// <para>This interface serves as security clearance for the runtime to allow IIS requests going to/from the class. </para>
    /// <para>Only methods marked with WebRestMethodAttribute will be included in the client and be callable from the client.</para>
    /// </summary>
    public interface IWebRestService
    {
    }

    public static class IWebRestServiceClientExtensions
    {
        /// <summary>
        /// Method builds URL that can be use from the Client to call specific method in a class with parameters.
        /// </summary>
        /// <param name="methodName">Method to call.</param>
        /// <param name="parameters">List of parameters (optional).</param>
        /// <returns>Returns URL.</returns>
        public static string GetUrlFunctionCall(this IWebRestService service, string methodName, params object[] parameters)
        {
            return WebRestClient.GetUrlFunctionCall(service.GetType(), methodName, parameters);
        }

        /// <summary>
        /// Method builds javascript that can be use from the Client to call specific method in a class with parameters.
        /// </summary>
        /// <param name="type">Type to call.</param>
        /// <param name="methodName">Method to call.</param>
        /// <param name="parameters">List of parameters (optional).</param>
        /// <returns>Returns URL.</returns>
        public static string GetJSClientFunctionCall(this IWebRestService service, string methodName, params object[] parameters)
        {
            return WebRestClient.GetJSClientFunctionCall(service.GetType(), methodName, parameters);
        }

        /// <summary>
        /// Method builds javascript that can be use from the Client to call specific method in a class with parameters without prefixing call with "javascript".
        /// </summary>
        /// <param name="type">Type to call.</param>
        /// <param name="methodName">Method to call.</param>
        /// <param name="parameters">List of parameters (optional).</param>
        /// <returns>Returns URL.</returns>
        public static string GetJSClientFunctionCallUnwrapped(this IWebRestService service, string methodName, params object[] parameters)
        {
            return WebRestClient.GetJSClientFunctionCallUnwrapped(service.GetType(), methodName, parameters);
        }

        /// <summary>
        /// Method builds javascript that can be use from the Client to call specific method in a class with parameters 
        /// and registers callback javascript.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="methodName"></param>
        /// <param name="javascript"></param>
        /// <param name="parameters">List of parameters (optional).</param>
        /// <returns></returns>
        public static string GetJSClientFunctionCallWithCallback(this IWebRestService service, string methodName, string javascript, params object[] parameters)
        {
            return WebRestClient.GetJSClientFunctionCallWithCallback(service.GetType(), methodName, javascript, parameters);
        }

        /// <summary>
        /// Method builds javascript that can be use from the Client to call specific method in a class with parameters without prefixing call with "javascript"
        /// and registers callback javascript.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="methodName"></param>
        /// <param name="javascript"></param>
        /// <param name="parameters">List of parameters (optional).</param>
        /// <returns></returns>
        public static string GetJSClientFunctionCallUnwrappedWithCallback(this IWebRestService service, string methodName, string javascript, params object[] parameters)
        {
            return WebRestClient.GetJSClientFunctionCallUnwrappedWithCallback(service.GetType(), methodName, javascript, parameters);
        }

        /// <summary>
        /// Method builds function name to use within client javascript by given type and method name.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="methodName"></param>
        /// <returns></returns>
        public static string GetJSClientFunctionName(this IWebRestService service, string methodName)
        {
            return WebRestClient.GetJSClientFunctionName(service.GetType(), methodName);
        }

        public static string GetJSClientDefaultCallbackName(this IWebRestService service, string methodName)
        {
            return WebRestClient.GetJSClientDefaultCallbackName(service.GetType(), methodName);
        }

        /// <summary>
        /// Method registers special javascript block for all class methods avilable for calling as rest service.
        /// </summary>
        /// <param name="type">Type to register.</param>
        /// <returns>Returns true if type was successfully registered; otherwise - false.</returns>
        public static bool RegisterJSClient(this IWebRestService service)
        {
            return WebRestClient.RegisterJSClient(service.GetType());
        }

        /// <summary>
        /// Method registers javascript callback block (on current page) for specified class and method with provided custom javascript code.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="method"></param>
        /// <param name="javascript"></param>
        /// <returns></returns>
        public static bool RegisterJSCallback(this IWebRestService service, string method, string javascript)
        {
            return WebRestClient.RegisterJSCallback(service.GetType(), method, javascript);
        }

        public static bool RegisterJSCallback(this IWebRestService service, string method, Templates.JavaScriptTemplateType javascriptTemplate)
        {
            return WebRestClient.RegisterJSCallback(service.GetType(), method, javascriptTemplate);
        }

        public static bool RegisterJSCallback(this IWebRestService service, string method, Templates.JavaScriptTemplateType javascriptTemplate, string templateParameter)
        {
            return WebRestClient.RegisterJSCallback(service.GetType(), method, javascriptTemplate, templateParameter);
        }
    }

}
